
*=============================================================================

*** Setup

version 15
capture log close
set more off
clear all

* Load data
use "$data/pooled_touse.dta"

* If needed, install ebalance program
findfile ebalance.ado
if (_rc){
	ssc install ebalance
}

*=============================================================================

*** Calculate weights

* Empty weight var to be populated
gen mbweight=.

* United States
/* Age: 18-29: 30%
Age: 30-44: 25%
Age: 45-64: 25%
Age: 65+: 20%
Education: Less than HS: 12%
Education: HS 28%
Education: Some College: 31%
Education: BA or higher: 29%
Gender: Male 48%
Gender: Female 52%
*/
ebalance ib4.agegroups ib4.educ female if country==1, manual(0.30 0.25 0.25 0.12 0.28 0.31 0.51) gen(mbweights_us)
foreach var in agegroups educ female {
di "US: NO WEIGHTS"
tab `var' if country==1
di "US: WITH WEIGHTS"
tab `var' [aweight=mbweights_us] if country==1
}
replace mbweight=mbweights_us if country==1

* United Kingdom
/* Age: 18-29: 22%
Age: 30-44: 26%
Age: 45-64: 30%
Age: 65+: 22%
Education: 16yrs or less 41%
Education: 17-18 28%
Education: 19+ 31%
Gender: Male 50%
Gender: Female 50%
*/
* Explore missing socdemo info
*mdesc agegroups educ_yrs female if country==2

ebalance ib4.agegroups ib3.educ_yrs female if country==2, manual(0.22 0.26 0.30 0.41 0.28 0.50) gen(mbweights_uk)
foreach var in agegroups educ_yrs female {
di "UK: NO WEIGHTS"
tab `var' if country==2
di "UK: WITH WEIGHTS"
tab `var' [aweight=mbweights_uk] if country==2
}
replace mbweight=mbweights_uk if country==2

* Germany
/* Age: 18-29: 19%
Age: 30-44: 21%
Age: 45-64: 35%
Age: 65+: 24%
Education: 16yrs or less 38%
Education: 17-18 19%
Education: 19+ 43%
Gender: Male 49%
Gender: Female 51%
*/
ebalance ib4.agegroups ib3.educ_yrs female if country==3, manual(0.19 0.21 0.35 0.38 0.19 0.51) gen(mbweights_ge)
foreach var in agegroups educ_yrs female {
di "Germany: NO WEIGHTS"
tab `var' if country==3
di "Germany: WITH WEIGHTS"
tab `var' [aweight=mbweights_ge] if country==3
}
replace mbweight=mbweights_ge if country==3

* France
/* Age: 18-29: 20%
Age: 30-44: 23%
Age: 45-64: 32%
Age: 65+: 26%
Education: 16yrs or less 26%
Education: 17-18 25%
Education: 19+ 49%
Gender: Male 47%
Gender: Female 53%
*/
ebalance ib4.agegroups ib3.educ_yrs female if country==4, manual(0.20 0.23 0.32 0.26 0.25 0.53) gen(mbweights_fr)
foreach var in agegroups educ_yrs female {
di "France: NO WEIGHTS"
tab `var' if country==4
di "France: WITH WEIGHTS"
tab `var' [aweight=mbweights_fr] if country==4
}
replace mbweight=mbweights_fr if country==4

*=============================================================================

*** Saving

* Save individual-level data
compress
save "$data/pooled_touse.dta", replace

* Split off weight variable
keep caseid mbweight
sort caseid
save "$data/pooled_mbweight_touse.dta", replace

* Load scenario-level pooled data for conjoint
use "$data/pooled_scenlevel_touse.dta", clear

* Merge survey weight ontop scenario-level data
merge m:1 caseid using "$data/pooled_mbweight_touse.dta"

* Save scenario-level data
compress
save "$data/pooled_scenlevel_touse.dta", replace

* Load US alternative scenario-level data for conjoint
use "$data/us_scenlevel_doubletiming_touse.dta", clear

* Merge survey weight ontop scenario-level data
merge m:1 caseid using "$data/pooled_mbweight_touse.dta"

* Save US alternative scenario-level data
compress
save "$data/us_scenlevel_doubletiming_touse.dta", replace
